"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = showWaveEffect;
var React = _interopRequireWildcard(require("react"));
var _rcMotion = _interopRequireDefault(require("rc-motion"));
var _render = require("rc-util/lib/React/render");
var _classnames = _interopRequireDefault(require("classnames"));
var _util = require("./util");
const WaveEffect = props => {
  const {
    className,
    left,
    top,
    width,
    height,
    color,
    borderRadius,
    scale
  } = props;
  const divRef = React.useRef(null);
  const waveStyle = {
    left,
    top,
    width,
    height,
    borderRadius: borderRadius.map(radius => `${radius}px`).join(' '),
    '--wave-scale': scale
  };
  if (color) {
    waveStyle['--wave-color'] = color;
  }
  return /*#__PURE__*/React.createElement(_rcMotion.default, {
    visible: true,
    motionAppear: true,
    motionName: "wave-motion",
    motionDeadline: 5000,
    onAppearEnd: (_, event) => {
      var _a;
      if (event.deadline || event.propertyName === 'opacity') {
        const holder = (_a = divRef.current) === null || _a === void 0 ? void 0 : _a.parentElement;
        (0, _render.unmount)(holder).then(() => {
          var _a;
          (_a = holder.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(holder);
        });
      }
      return false;
    }
  }, _ref => {
    let {
      className: motionClassName
    } = _ref;
    return /*#__PURE__*/React.createElement("div", {
      ref: divRef,
      className: (0, _classnames.default)(className, motionClassName),
      style: waveStyle
    });
  });
};
function validateNum(value) {
  return Number.isNaN(value) ? 0 : value;
}
function showWaveEffect(container, node, className) {
  const nodeStyle = getComputedStyle(node);
  const nodeRect = node.getBoundingClientRect();
  // Get wave color from target
  const waveColor = (0, _util.getTargetWaveColor)(node);
  // Get border radius
  const {
    borderTopLeftRadius,
    borderTopRightRadius,
    borderBottomLeftRadius,
    borderBottomRightRadius
  } = nodeStyle;
  // Do scale calc
  const {
    offsetWidth
  } = node;
  const scale = validateNum(nodeRect.width / offsetWidth);
  // Create holder
  const holder = document.createElement('div');
  (0, _util.getValidateContainer)(container).appendChild(holder);
  (0, _render.render)( /*#__PURE__*/React.createElement(WaveEffect, {
    left: nodeRect.left,
    top: nodeRect.top,
    width: nodeRect.width,
    height: nodeRect.height,
    color: waveColor,
    className: className,
    scale: scale,
    borderRadius: [borderTopLeftRadius, borderTopRightRadius, borderBottomRightRadius, borderBottomLeftRadius].map(radius => validateNum(parseFloat(radius) * scale))
  }), holder);
}